import enquireJs from 'enquire.js'

export function isDef (v){
  return v !== undefined && v !== null
}

/**
 * Remove an item from an array.
 */
export function remove (arr, item) {
  if (arr.length) {
    const index = arr.indexOf(item)
    if (index > -1) {
      return arr.splice(index, 1)
    }
  }
}

export function isRegExp (v) {
  return _toString.call(v) === '[object RegExp]'
}

export function enquireScreen(call) {
  const handler = {
    match: function () {
      call && call(true)
    },
    unmatch: function () {
      call && call(false)
    }
  }
  enquireJs.register('only screen and (max-width: 767.99px)', handler)
}

const _toString = Object.prototype.toString


export function deepCopyArray(arr) {
  if (!Array.isArray(arr)) {
    throw new Error("Input is not an array");
  }

  const copy = [];

  for (let i = 0; i < arr.length; i++) {
    if (Array.isArray(arr[i])) {
      copy[i] = deepCopyArray(arr[i]);
    } else if (typeof arr[i] === 'object' && arr[i] !== null) {
      copy[i] = deepCopyObject(arr[i]);
    } else {
      copy[i] = arr[i];
    }
  }

  return copy;
}

export function deepCopyObject(obj) {
  if (typeof obj !== 'object' || obj === null) {
    return obj;
  }

  const copy = {};

  for (const key in obj) {
    if (Array.isArray(obj[key])) {
      copy[key] = deepCopyArray(obj[key]);
    } else if (typeof obj[key] === 'object' && obj[key] !== null) {
      copy[key] = deepCopyObject(obj[key]);
    } else {
      copy[key] = obj[key];
    }
  }

  return copy;
}